package io.renren.controller;

import cn.hutool.core.collection.CollectionUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.entity.ApEntity;
import io.renren.param.ApHeartBeatData;
import io.renren.param.ApHeartBeatParam;
import io.renren.service.ApService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 基础
 *
 * @author 1
 * @email 1@2.com
 * @date 2021-09-17 15:57:01
 */
@RestController
@RequestMapping("/ap")
public class ApController {
    @Autowired
    private ApService apService;

    /**
     * 列表，返回未绑定用户或本公司的ap
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isBlank((String) params.get("user"))) {
            List<String> users = new ArrayList<>();
            users.add("default");
            users.add((String) params.get("compId"));
            params.put("users", users);
        }
        PageUtils page = apService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ApEntity ap = apService.getById(id);

        return R.ok().put("ap", ap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ApHeartBeatParam param) {

        /*{
            "status_no": "0",
                "type": "AP_STATUS",
                "data": [{
            "ap_id": "2",
                    "listen_port": "0",
                    "online_begin_time": "",
                    "ip": "192.168.9.253",
                    "channel": "NONE",
                    "version": "2",
                    "work_mode": "standby",
                    "mac": "DC:07:C1:01:01:4C",
                    "sequence": "14",
                    "netmask": "NONE",
                    "last_work_time": "",
                    "sn": "22222222222",
                    "user": "default",
                    "gateway": "NONE",
                    "status": "offline",
                    "desc": "NONE"
        }]
        }*/

        List<ApEntity> apEntityList = new ArrayList<>();
        for (ApHeartBeatData datum : param.getData()) {
            String apId = datum.getApId();
            Map<String, Object> map = new HashMap<>();
            map.put("apId", apId);

            PageUtils aps = apService.queryPage(map);
            if (CollectionUtil.isNotEmpty(aps.getList())) {
                //ap 已存在，更新
                for (ApEntity ap : (List<ApEntity>) aps.getList()) {
                    Long id = ap.getId();
                    ap = datum.convertFor(datum);
                    ap.setId(id);
                    /*ap.setChannel(datum.getChannel());
                    ap.setDesc(datum.getDesc());
                    ap.setGateway(datum.getGateway());
                    ap.setIp(datum.getIp());
                    ap.setLastWorkTime(datum.getLastWorkTime());
                    ap.setListenPort(datum.getListenPort());
                    ap.setMac(datum.getMac());
                    ap.setNetmask(datum.getNetmask());
                    ap.setOnlineBeginTime(datum.getOnlineBeginTime());
                    ap.setSequence(datum.getSequence());
                    ap.setSn(datum.getSn());
                    ap.setStatus(datum.getStatus());
                    ap.setType(param.getType());
                    ap.setUser(datum.getUser());
                    ap.setVersion(datum.getVersion());
                    ap.setWorkMode(datum.getWorkMode());*/
                    apService.updateById(ap);
                }
            } else {
                //不存在，新增
                ApEntity apEntity = datum.convertFor(datum);
                apEntity.setStatusNo(param.getStatusNo());
                apEntity.setType(param.getType());
                apEntityList.add(apEntity);
            }
        }
        apService.saveOrUpdateBatch(apEntityList);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ApEntity ap) {
        ValidatorUtils.validateEntity(ap);
        apService.updateById(ap);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        apService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
